﻿using System;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window3.xaml which is where the bogusanswers will be added to each regular answer
    /// </summary>
    public partial class Window3 : Window
    {
        private Assignment myAssignment;
        private int i;

        public Window3(Assignment assignment)
        {
            InitializeComponent();
            AddAssignment(assignment);
            i = 0;
            textBox2.Text = myAssignment.AssignmentText;
            updateTextBlock();
            findButtonName();
            updateListbox();
        }

        /// <summary>
        /// Method used for passing an assignment from the second step window to this one.
        /// </summary>
        public void AddAssignment(Assignment assignment)
        {
            myAssignment = assignment;
        }

        /// <summary>
        /// Goes on to the next answer if there are more answers in the list, if not it will finish creating the 
        /// assignment, put it up on the database and close the window.
        /// </summary>
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if(i < myAssignment.GetAnswerlist().Count-1)
            {
                i++;
                updateTextBlock();
                findButtonName();
                updateListbox();
            }
            else
            {
                foreach(var ans in myAssignment.GetAnswerlist())
                {
                    int j = myAssignment.AssignmentText.IndexOf(" ________________ ");
                    myAssignment.AssignmentText = myAssignment.AssignmentText.Remove(j, 18);
                    myAssignment.AssignmentText = myAssignment.AssignmentText.Insert(j, "???");
                }
                Queries.CreateAssignment(myAssignment);
                Close();
            }   
        }
        
        /// <summary>
        /// Updates the textblock that tells what the current answer is.
        /// </summary>
        private void updateTextBlock()
        {
            textBlock2.Text = "Nuværende ord: " + myAssignment.GetAnswerlist()[i].Realanswer;
        }

        /// <summary>
        /// Updates the listbox with the list of bogusAnswers
        /// </summary>
        private void updateListbox()
        {
            if (myAssignment.GetType() == 1)
            {
                addRealAnswerToBogusList(myAssignment.GetAnswerlist()[i]);    
            }
            listBox1.ItemsSource = myAssignment.GetAnswerlist()[i].GetBogusAnswers();
        }

        /// <summary>
        /// Sets the name of the button2 depending if its the last answer in the list or not
        /// </summary>
        private void findButtonName()
        {
            if(i == myAssignment.GetAnswerlist().Count-1)
            {
                button2.Content = "Færdiggør ny opgave";
            }
            else
            {
                button2.Content = "Næste ord";
            }
        }

        /// <summary>
        /// Adds a bogus answer to a list in a answer if the requirements for that type of assignment is met
        /// </summary>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            switch (myAssignment.GetType())
            {
                case 0:
                    if(textBox1.Text.Trim() != "" && textBox1.Text.Length < 17
                        && myAssignment.GetAnswerlist()[i].GetBogusAnswers().Contains(textBox1.Text) == false &&
                        myAssignment.GetAnswerlist()[i].Realanswer != textBox1.Text)
                    {
                        myAssignment.GetAnswerlist()[i].AddBogusAnswer(textBox1.Text);
                        listBox1.Items.Refresh();
                        textBox1.Text = "";
                    }
                    else
                        MessageBox.Show("Mulige fejl:"
                             + Environment.NewLine + Environment.NewLine + 
                             "Tekstboxen der skal indeholde det du vil tilføje er enten tom eller kun udfyldt af mellemrum"
                              + Environment.NewLine + Environment.NewLine + 
                              "Længden på teksten skal være under 17 tegn lang"
                               + Environment.NewLine + Environment.NewLine + 
                               "Svarmuligheden findes allerede.");
                    break;
                case 1:
                    if (textBox1.Text != ""
                        && myAssignment.GetAnswerlist()[i].GetBogusAnswers().Contains(textBox1.Text) == false
                        && textBox1.Text != " " && textBox1.Text.Length == 1)
                    {
                        myAssignment.GetAnswerlist()[i].AddBogusAnswer(textBox1.Text);
                        listBox1.Items.Refresh();
                        textBox1.Text = "";
                    }
                    else
                        MessageBox.Show("Mulige fejl:"
                             + Environment.NewLine + Environment.NewLine + 
                             "Tekstboxen der skal indeholde det du vil tilføje er enten tom eller kun udfyldt af mellemrum"
                              + Environment.NewLine + Environment.NewLine +  
                              "Da dette er en stavelsesøvelse må hver svarmulighed kun indeholde et bogstav."
                              + Environment.NewLine + Environment.NewLine +  
                              "Svarmuligheden findes allerede");
                    break;
            }
        }

        /// <summary>
        /// Is used to add all the letters of the correct answer to the answer list if the assignmenttype is 
        /// a spelling assignment
        /// </summary>
        /// <param name="answer"></param>
        public void addRealAnswerToBogusList(Answer answer)
        {
            foreach (char c in answer.Realanswer)
            {
                if(answer.GetBogusAnswers().Contains(c.ToString()) == false)
                answer.AddBogusAnswer(c.ToString());
            }
        }

        /// <summary>
        /// Used to remove a bogusanswer from the answerlist
        /// </summary>
        private void button3_Click(object sender, RoutedEventArgs e)
        {
                    myAssignment.GetAnswerlist()[i].RemoveBogusAnswer(listBox1.SelectedItem.ToString(), myAssignment.GetType());
                    listBox1.Items.Refresh();
        }

        /// <summary>
        /// Help button telling how the current window is used.
        /// </summary>
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Dette er det sidste trin i oprettelsen af en opgave. For hver del af teksten der skulle være skjult for eleven"
                + ", skal der nu være andre svarmuligheder. Disse tilføjes ved at skrive en svarmulighed oppe i venstre hjørne og trykke ok" +
                System.Environment.NewLine + System.Environment.NewLine + "Ønsker man at fjerne en svarmulighed trykker man på den i den store liste i midten,"
                + " hvorefter man trykker på knappen 'Slet svarmulighed'." + System.Environment.NewLine + System.Environment.NewLine +
                "Når alle ønskede svarmuligheder er tilføjet til et ord trykkes der på 'Næste ord' knappen. Dette gør man indtil 'Næste ord' "
                + "knappen hedder 'Færdiggør ny opgave'. Når den gør dette er man nået til sidste ord og opgaven vil blive oprettet når man trykker på knappen.");
        }
    }
}
